package Search;

import java.util.ArrayList;
import java.util.LinkedList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.cloudgarden.resource.SWTResourceManager;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * GUI for the Tag Search
 */
public class NewSWTApp extends org.eclipse.swt.widgets.Composite {

	private Button addButton;
	private Composite composite1;
	private ScrolledComposite scrolledComposite1;
	private Label pleaseLabel;
	private Button searchButton;
	private List resultsList;
	private Label resultsLabel;
	private Display display;
	private Composite parent;

	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}

	/**
	 * Constructor for the NewSWTApp class
	 * @param parent the parent compostie
	 * @param style the app style
	 * @param display the display used
	 */
	public NewSWTApp(Composite parent, int style, Display display) {
		super(parent, style);
		this.display=display;
		this.parent=parent;
		initGUI();
	}
	
	private ArrayList<SearchParam> paramList=new ArrayList<SearchParam>();
	
	/**
	 * Class to represent a search parameter widget
	 */
	private class SearchParam {
		private Composite comp;
		private Text text;
		private Button box;
		
		/**
		 * Constructor for the SearchParam class
		 * @param comp the composite in which to make the widget
		 */
		public SearchParam (Composite comp){
			this.comp=comp;
			text=new Text(comp, SWT.BORDER);
			GridData textLData = new GridData();
			text.setLayoutData(textLData);
			box=new Button(comp, SWT.CHECK | SWT.LEFT);
			GridData boxLData = new GridData();
			box.setLayoutData(boxLData);
			box.setBackground(SWTResourceManager.getColor(186, 228, 228));
			box.setText("not");
		}
		
		/**
		 * @return the text parameter
		 */
		public String getText () {
			return text.getText();
		}
		
		/**
		 * @return the yesOrNo parameter
		 */
		public boolean getChecked () {
			return box.getSelection();
		}
	}
	
	/**
	* Initializes the GUI.
	*/
	private void initGUI() {
		try {
			this.setSize(400, 170);
			this.setBackground(SWTResourceManager.getColor(107, 198, 199));
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			{
				FormData resultsListLData = new FormData();
				resultsListLData.left =  new FormAttachment(0, 1000, 24);
				resultsListLData.top =  new FormAttachment(0, 1000, 185);
				resultsListLData.width = 350;
				resultsListLData.height = 103;
				resultsList = new List(this, SWT.BORDER);
				resultsList.setLayoutData(resultsListLData);
				resultsList.setVisible(false);
			}
			{
				resultsLabel = new Label(this, SWT.NONE);
				FormData resultsLabelLData = new FormData();
				resultsLabelLData.left =  new FormAttachment(0, 1000, 24);
				resultsLabelLData.top =  new FormAttachment(0, 1000, 158);
				resultsLabelLData.width = 40;
				resultsLabelLData.height = 15;
				resultsLabel.setLayoutData(resultsLabelLData);
				resultsLabel.setText("Results:");
				resultsLabel.setVisible(false);
				resultsLabel.setBackground(SWTResourceManager.getColor(107, 198, 199));
			}
			{
				searchButton = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData searchButtonLData = new FormData();
				searchButtonLData.left =  new FormAttachment(0, 1000, 207);
				searchButtonLData.top =  new FormAttachment(0, 1000, 133);
				searchButtonLData.width = 170;
				searchButtonLData.height = 25;
				searchButton.setLayoutData(searchButtonLData);
				searchButton.setText("Search!");
				searchButton.setBackground(SWTResourceManager.getColor(107, 198, 199));
				searchButton.addSelectionListener(new SelectionListener(){
					@Override
					public void widgetDefaultSelected(SelectionEvent arg0) {}
					@Override
					public void widgetSelected(SelectionEvent arg0) {
						submitSearch();
					}
				});
			}
			{
				addButton = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData addButtonLData = new FormData();
				addButtonLData.left =  new FormAttachment(0, 1000, 311);
				addButtonLData.top =  new FormAttachment(0, 1000, 12);
				addButtonLData.width = 77;
				addButtonLData.height = 25;
				addButton.setLayoutData(addButtonLData);
				addButton.setText("+ Parameter");
				addButton.setBackground(SWTResourceManager.getColor(107, 198, 199));
				addButton.addSelectionListener(new SelectionListener(){
					@Override
					public void widgetDefaultSelected(SelectionEvent arg0) {}
					@Override
					public void widgetSelected(SelectionEvent arg0) {
						Composite comp=new Composite(composite1, SWT.NONE);
						GridLayout compLayout = new GridLayout();
						compLayout.makeColumnsEqualWidth = true;
						RowData compLData = new RowData();
						compLData.width = 106;
						compLData.height = 50;
						comp.setLayoutData(compLData);
						comp.setLayout(compLayout);
						comp.setBackground(SWTResourceManager.getColor(186, 228, 228));
						paramList.add(new SearchParam(comp));
						composite1.setSize(composite1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
						display.update();
					}		
				});
			}
			{
				FormData scrolledComposite1LData = new FormData();
				scrolledComposite1LData.left =  new FormAttachment(0, 1000, 12);
				scrolledComposite1LData.top =  new FormAttachment(0, 1000, 39);
				scrolledComposite1LData.width = 372;
				scrolledComposite1LData.height = 61;
				scrolledComposite1 = new ScrolledComposite(this, SWT.H_SCROLL | SWT.BORDER);
				scrolledComposite1.setLayoutData(scrolledComposite1LData);
				scrolledComposite1.setBackground(SWTResourceManager.getColor(186, 228, 228));
				{
					composite1 = new Composite(scrolledComposite1, SWT.NONE);
					RowLayout composite1Layout = new RowLayout();
					composite1.setLayout(composite1Layout);
					scrolledComposite1.setContent(composite1);
					composite1.setBounds(0, 0, 372, 64);
					composite1.setBackground(SWTResourceManager.getColor(186, 228, 228));
					for (int i=0; i<3; i++){
						Composite comp=new Composite(composite1, SWT.NONE);
						GridLayout compLayout = new GridLayout();
						compLayout.makeColumnsEqualWidth = true;
						RowData compLData = new RowData();
						compLData.width = 106;
						compLData.height = 50;
						comp.setLayoutData(compLData);
						comp.setLayout(compLayout);
						comp.setBackground(SWTResourceManager.getColor(186, 228, 228));
						paramList.add(new SearchParam(comp));
					}
					composite1.setSize(composite1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
					display.update();
				}
			}
			{
				pleaseLabel = new Label(this, SWT.NONE);
				FormData pleaseLabelLData = new FormData();
				pleaseLabelLData.left =  new FormAttachment(0, 1000, 12);
				pleaseLabelLData.top =  new FormAttachment(0, 1000, 12);
				pleaseLabelLData.width = 260;
				pleaseLabelLData.height = 15;
				pleaseLabel.setLayoutData(pleaseLabelLData);
				pleaseLabel.setText("Please enter the tags you would like to search for.");
				pleaseLabel.setBackground(SWTResourceManager.getColor(107, 198, 199));
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Submit search and display results
	 */
	private void submitSearch (){
		LinkedList <Parameter> params=new LinkedList<Parameter>();
		//convert the parameter widgets to search parameters
		for (SearchParam sp:paramList){
			params.add(new Parameter(sp.getText(), sp.getChecked()));
		}
		//send parameters to search control
		java.util.List<String> results=Controller.search(params);
		Point size=new org.eclipse.swt.graphics.Point(400,300);
		this.setSize(size);
		if (parent instanceof Shell)
		{
			parent.setLayout(new FillLayout());
			parent.layout();
			Rectangle shellBounds = parent.computeTrim(0, 0, size.x, size.y);
			parent.setSize(shellBounds.width, shellBounds.height);
		}
		//display results
		resultsLabel.setVisible(true);
		resultsList.setVisible(true);
		resultsList.removeAll();
		if (results!=null)
			for (String res:results)
				resultsList.add(res);
		display.update();
	}
	
	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		shell.setText("Search Clever Dropbox");
		Image image=SWTResourceManager.getImage("tiffany-blue-box.jpg");
		if (image!=null)
			shell.setImage(image);
		NewSWTApp inst = new NewSWTApp(shell, SWT.NULL, display);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
}